<template>
    <div>
        <el-row>
            <el-col :span="24">
                <el-form ref="form" :rules="rules"  :model="form" label-width="120px">
                    <el-form-item label="用户名" prop="username">
                        <el-input readonly v-model="form.username" placeholder="用户登录使用的用户名"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input v-model="form.password" show-password placeholder="若不修改请勿填写"></el-input>
                    </el-form-item>
<!--                    <el-form-item label="真实姓名" prop="name">-->
<!--                        <el-input v-model="form.name" placeholder="显示名称"></el-input>-->
<!--                    </el-form-item>-->
<!--                    <el-form-item label="邮箱" prop="email">-->
<!--                        <el-input v-model="form.email" placeholder="联系邮箱, 方便发送邮件报警等通知"></el-input>-->
<!--                    </el-form-item>-->
                    <el-form-item label="手机" prop="mobile">
                        <el-input v-model="form.mobile" placeholder="手机号码"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" :loading="loading" @click="submit">确定</el-button>
                        <el-button @click="cancel">返回</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    export default {
        props: {
            data: {

            }
        },
        data() {
            return {
                loading: false,
                form: {},
                formDefault: {
                    id: 0,
                    username: '',
                    password: '',
                    name: '',
                    // email: '',
                    mobile: '',
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                    ],
                },
            }
        },
        computed: {
            roles () {
                return this.$store.state.role.all;
            }
        },
        watch: {
            data (newVal) {
                this.syncForm();
            }
        },
        methods: {
            getFormAction() {
                return 'manager/UpdateSimple';
            },
            submit() {
                this.$refs.form.validate(valid => {
                    if (!valid) return false;
                    this.loading = true;
                    delete this.form.roles
                    this.$store.dispatch(this.getFormAction(), this.form).then(res => {
                        this.$message.success(res.data.message);
                        this.$store.dispatch('manager/Info');
                        this.$emit('submit');
                    }).finally(() => {
                        this.loading = false;
                    });
                });
            },
            cancel() {
                this.$emit('cancel');
            },
            syncForm() {
                this.form = Object.assign({}, this.$store.state.manager.info || this.formDefault);
                if (this.form.id) delete this.rules['password'];
            },
        },
        created () {
            this.syncForm();
        }
    }
</script>

<style lang="scss" scoped>
.item-row {
    margin: 0 0 10px 0;
}
.avatar-uploader {
    width: 100px;
    height: 100px;
    border: 1px dashed grey;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
}
form .el-input {
    width: 150px;
}
</style>

